<template>
  <div>
      <el-card>
        <div class="add-cate-row">
          <el-button type="danger" @click="addFirstCate">添加一级分类</el-button>
        </div>
        <div class="cate-wrap">
          <div class="cate-list">
            <!-- 每一项分类 -->
            <div class="cate-item" v-for="item in cateList.list" :key="item.id">
              <h4 class="item-title">
                <i class="el-icon-remove-outline flex-icon"></i>
                <span>{{item.category_name}}</span>
                <div class="btn-group">
                  <el-button type="danger"  size="mini" round>编辑</el-button>
                  <el-button type="success" size="mini" round @click="addSecCate(item)">添加子集</el-button>
                  <el-button size="mini" round>删除</el-button>
                </div>
                </h4>
              <ul class="subitem-list" v-if="item.children">
                <li class="subitem" v-for="subitem in item.children" :key="subitem.id">
                  {{subitem.category_name}}
                   <div class="btn-group">
                  <el-button type="danger"  size="mini" round>编辑</el-button>
                  <el-button size="mini" round>删除</el-button>
                </div>
                  </li>
              </ul>
            </div>
          </div>
          <div class="cate-edit">
            <h4>一级分类编辑</h4>
            <el-form ref="cateForm" :model="cateForm" label-width="100px">
  <el-form-item label="一级分类名称">
    <el-input v-model="cateForm.categoryName" :disabled="firstCateIptStatus"></el-input>
  </el-form-item>
  <el-form-item label="二级分类名称">
    <el-input v-model="cateForm.secCategoryName" :disabled="secCateIptStatus"></el-input>
  </el-form-item>
   <el-form-item>
    <el-button type="danger" :disabled="submitBtnStatus" @click="submitForm" :loading="loading">提交</el-button>
  </el-form-item>
            </el-form>
          </div>
        </div>
      </el-card>
  </div>
</template>

<script>
import { reactive, onMounted, ref } from '@vue/composition-api'
import { getInfoCateAll, addFirstInfoCate, addSecInfoCate } from '@/api/info.js'
export default {
  setup (props, { root }) {
    // 新增分类和编辑分类表单
    const firstCateIptStatus = ref(true)
    const secCateIptStatus = ref(true)
    const submitBtnStatus = ref(true)
    const loading = ref(false)
    let formType = ''
    const cateForm = reactive({
      categoryName: '',
      secCategoryName: '',
      parentId: ''
    })
    // 点击添加一级分类按钮
    const addFirstCate = async () => {
      resetForm()
      formType = 'add'
      firstCateIptStatus.value = false
      submitBtnStatus.value = false
    }
    // 点击添加子集
    const addSecCate = (item) => {
      resetForm()
      formType = 'addSec'
      submitBtnStatus.value = false
      secCateIptStatus.value = false
      cateForm.parentId = item.id
      cateForm.categoryName = item.category_name
      console.log(cateForm)
    }
    // 重置表单
    const resetForm = () => {
      firstCateIptStatus.value = true
      secCateIptStatus.value = true
      submitBtnStatus.value = true
      cateForm.categoryName = ''
      cateForm.parentId = ''
      cateForm.secCategoryName = ''
    }
    // 提交表单
    const submitForm = async () => {
      let result = {}
      loading.value = true
      // 添加分类
      if (formType === 'add') {
        console.log(cateForm)
        if (!cateForm.categoryName) {
          return root.$message({
            message: '输入不能为空',
            type: 'error'
          })
        }
        const requestParams = {
          categoryName: cateForm.categoryName
        }
        const { data: res } = await addFirstInfoCate(requestParams)
        result = res
      } else if (formType === 'addSec') {
        // 添加子集分类
        if (!cateForm.secCategoryName) {
          return root.$message({
            message: '输入不能为空',
            type: 'error'
          })
        }
        const requestParams = {
          categoryName: cateForm.secCategoryName,
          parentId: cateForm.parentId
        }
        // console.log(requestParams)
        const { data: res } = await addSecInfoCate(requestParams)
        result = res
      }
      if (result.resCode === 0) {
        root.$message({
          type: 'success',
          message: result.message
        })
        getCateList()
      }
      loading.value = false
      resetForm()
    }
    // 分类列表
    const cateList = reactive({
      list: []
    })
    const getCateList = async () => {
      const { data: res } = await getInfoCateAll({})
      console.log(res)
      cateList.list = res.data
    }
    // 生命周期
    onMounted(() => {
      getCateList()
    })
    return {
      cateForm,
      cateList,
      firstCateIptStatus,
      secCateIptStatus,
      submitBtnStatus,
      addFirstCate,
      submitForm,
      loading,
      addSecCate
    }
  }
}
</script>

<style lang="scss" scoped>
  .add-cate-row {
    position: relative;
    padding: 15px;
    &::before {
      content: "";
      position: absolute;
    bottom: 0;
    // 定位 没有给宽 left 0 right 0 自动拉伸到与父定位元素一样长
    left: 0;
    right: 0;
    border-bottom: 1px solid #ccc;
    // margin负值 反方向移动 但没有具体宽度 拉长
    margin-right: -10px;
    }
  }
  .cate-wrap {
    display: flex;
    margin-top: 15px;
    font-size: 12px;
  }
  .cate-list {
    flex: 1;
    .cate-item {
      position: relative;
      &::before {
        position: absolute;
        content: "";
        left: 27px;
        top: 18px;
        bottom: 0;
        border-right: 1px dotted #ccc;
      }
      .btn-group {
        display: none;
        float: right;
      }
    h4 {
      position: relative;
      padding:  0 10px 0 40px;
      line-height: 40px;
      transition: all .2s;
      .flex-icon {
        position: absolute;
        top: 13px;
        left: 21px;
        background-color: #fff;
      }
      &:hover {
        background-color: #efefef;
        .btn-group {
          display: block;
        }
      }
    }
    li {
      position: relative;
      padding-left: 30px;
      margin-left: 30px;
      line-height: 40px;
      transition: all .2s;
      &::before {
        content: "";
        position: absolute;
        left: 0;
        top: 20px;
        width: 30px;
        border-bottom: 1px dotted #ccc;
      }
      &:hover {
        background-color: #efefef;
        .btn-group {
          display: block;
        }
      }
    }
  }
  }
  .cate-edit {
    flex: 2
  }
  .cate-edit {
    h4 {
      padding-left: 20px;
      line-height: 40px;
      background-color: #efefef;
    }
    .el-form {
      margin-top: 20px;
    }
  }
</style>
